package git.soulbgm.security.session;

import git.soulbgm.common.constant.HttpStatus;
import git.soulbgm.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.web.session.InvalidSessionStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 会话(session)无效时的策略类
 * 发生的情况是有可能别人把你的账号顶掉了 或 被管理员强制下线了
 *
 * @author SoulBGM
 * @date 2020-08-03
 */
@Slf4j
@Component
public class InvalidSessionStrategyImpl implements InvalidSessionStrategy {

    @Override
    public void onInvalidSessionDetected(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        int code = HttpStatus.UNAUTHORIZED;
        String msg = "会话无效,请重新登录";
        ResponseUtil.renderError(response, code, msg);
    }
}
